package com.mingtu.jgspatrol.fragment;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.mingtu.common.Eventbus.MyEventBus;
import com.mingtu.common.adapter.MyFragmentPagerAdapter;
import com.mingtu.common.base.BaseBindingFragment;
import com.mingtu.common.entity.TabEntity;
import com.mingtu.common.viewpager.LazyViewPager.LazyFragmentPagerAdapter;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.databinding.FragmentReceiveTaskBinding;
import com.mingtu.jgspatrol.utils.MyConstant;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

public class ReceiveTaskFragment extends BaseBindingFragment<FragmentReceiveTaskBinding> implements LazyFragmentPagerAdapter.Laziable {

    private static final String[] mTitles = {"进行中", "已完成"};
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private ReceiveUndoTaskFragment receiveUndoTaskFragment;
    private ReceiveFinishFragment receiveFinishFragment;

    public static ReceiveTaskFragment getInstance() {
        ReceiveTaskFragment fragment = new ReceiveTaskFragment();
        return fragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_receive_task;
    }

    @Override
    protected void initData() {

        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }
        receiveUndoTaskFragment = ReceiveUndoTaskFragment.getInstance();
        receiveFinishFragment = ReceiveFinishFragment.getInstance();
        mFragments.add(receiveUndoTaskFragment);
        mFragments.add(receiveFinishFragment);

        binding.viewPager.setOffscreenPageLimit(2);
        binding.viewPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), mFragments));
        binding.viewPager.setCurrentItem(0);
        binding.viewPager.addOnPageChangeListener(onPageChangeListener);

        binding.commonTab.setTextBold(1);
        binding.commonTab.setTabData(mTabEntities);
        binding.commonTab.setOnTabSelectListener(onTabSelectListener);

        EventBus.getDefault().register(this);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void getData() {

    }

    @Subscribe
    public void onStringEvent(MyEventBus event) {
        //业务逻辑
        String message = event.getMessgae();
        if (message.equals(MyConstant.REFRESH_RECEIVE_TASK)) {
            refreshReceiveUndoTaskData();
            refreshFinishTaskData();
        }
    }

    public void refreshReceiveUndoTaskData() {
        //会有获取不到的bug
        try {
            receiveUndoTaskFragment.refresh();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void refreshFinishTaskData() {
        receiveFinishFragment.refresh();
    }

    private OnTabSelectListener onTabSelectListener = new OnTabSelectListener() {
        @Override
        public void onTabSelect(int position) {
            binding.viewPager.setCurrentItem(position, false);
        }

        @Override
        public void onTabReselect(int position) {

        }
    };

    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            binding.commonTab.setCurrentTab(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

}